package iso
{
	import com.fminutes.vinciIsometric.core.IsoConst.IsoType;
	import com.fminutes.vinciIsometric.core.IsoObject;
	import com.fminutes.vinciIsometric.core.IsoUtils;
	import com.fminutes.vinciIsometric.core.IsoWorld;
	import com.fminutes.vinciIsometric.pathfinding.ANode;
	import com.fminutes.vinciIsometric.pathfinding.Astar;
	import com.fminutes.vinciIsometric.pathfinding.MapGrid;
	
	import flash.events.TimerEvent;
	import flash.geom.Vector3D;
	import flash.utils.Timer;
	
	import utils.Cube;
	import utils.color.randomColor;
	
	public class PlayerIsoObject extends IsoObject
	{
		
		private var _movingTimer:Timer;
		private var _movingPath:Array;
		
		
		
		public function PlayerIsoObject(floorWidth:Number, floorHeight:Number)
		{
			super(floorWidth, floorHeight);
			_movingTimer = new Timer(500);
			_movingTimer.addEventListener(TimerEvent.TIMER, onMovingTimer);	
			type = IsoType.MOVING;
		}
		
		private function onMovingTimer(event:TimerEvent):void
		{
			if (event.target.currentCount - 1 < _movingPath.length)
			{
				var node:ANode = _movingPath[event.target.currentCount - 1];
				xyz = new Vector3D(node.x * Main.ISO_WORLD_SIZE, 0, node.y * Main.ISO_WORLD_SIZE);
			}else
			{
				stop();
			}
		}
		
		public function move(_isoWorld:CityIsoWorld):void
		{
			var astar:Astar = new Astar();
			var map:MapGrid = _isoWorld.pathfindingMap;
			map.setStartNode(x / Main.ISO_WORLD_SIZE, z / Main.ISO_WORLD_SIZE);
			//鼠标是否在isoworld中
			if (_isoWorld.isPositionInIsoWorld(_isoWorld.getMousePosition())) 
			{
				map.setEndNode(_isoWorld.getMousePosition().x, _isoWorld.getMousePosition().z);
				if (astar.findPath(map))
				{
					_movingPath = astar.path;
					_movingTimer.start();
				}
			}
		}
		
		/**
		 *停止移动 
		 * 
		 */
		public function stop():void
		{
			_movingTimer.reset();
		}
		
		public static function produceRandomCubePlayer():PlayerIsoObject
		{
			var player:PlayerIsoObject = new PlayerIsoObject(Main.ISO_WORLD_SIZE, Main.ISO_WORLD_SIZE);
			player.displayChild = Cube.produceRandomCube();
			player.xyz = new Vector3D(0, 0, 0);
			return player;
		}
	}
}